What is an Object? What is a Class?

Forget programming for a second. If you think about the question above, you’ll realize how deceptively complex this question is. At first, the question looks easy. An object is… a “thing”. It’s an ​object in our world. At the same time, this means so much. For our purposes, let’s just define an object as any entity in the real world. Some examples include a person, a dog, a book, a hat, a boat, a state, a continent, a government, a planet, a galaxy, an atom, a poster, a wall, a building… you get the idea. Usually we define this as an object ​is an ​instance of a class.

Now what’s a ​class? That’s a bit trickier. Basically, a class is the core representation of an object. Any given object is an instance of a ​class. To give an example, “Dog” is a class. Any given dog that you encounter is an object, an instance of a dog. Such as the type of dog or attributes of a dog. Objects are a way to ​encapsulate information, meaning that a single variable contains all the needed data to describe something, rather than having to assign each piece of data to a different variable.

Now think about programming again. What does this mean for us as programmers? A programming “class” is simply the abstract representation of a real world object. So we’ll really have the same kinds of things in programming (a person, a dog, a book, a state, a planet… you get the idea). However, in order to create a digital representation of a physical object, you have to break it down into what’s important. In general, you have two different concepts to worry about.

1) ​Attributes - What does an object know/have? ​These are facts about your object. These can change (sometimes), but they are intended to hold information about your object. They are typically kept as variables within your class.
2) ​Methods - What does an object do? ​These are abilities of your object, or tasks it can complete. These are built into your object, but often act based on attributes. In C++, they are methods in your class.

Let’s think about an example: a Dog. Yes, a dog is pretty complicated, but we’re just making a representation of a dog, so it only needs to be as complicated as we need.

First, what ​attributes does a dog have? It could have a name, breed, height, weight, fur color, eye color, collar color, and many other attributes. However, for our purposes, the only one we’ll worry about is a dog’s name.

Second, what ​methods does a dog have? Dogs bark, eat, run, walk, fetch, sit, lay down, play dead, shake, and do many other tasks. However, for our purposes, the only one we’ll worry about is a dog’s ability to bark.

So, a dog ​has a name, and a dog ​can bark.

TIP: Look for words that highlight ownership (has, have, etc.) in a description to find

​ 	attributes. Look for action verbs to find​methods. 

More on Classes and Objects in C++

Objects need a method to be​ created. This is a special method known as a ​constructor.

Classes are the templates for objects, they define the ​attributes and ​methods an object has. The particular values for ​attributes and the ​methods called are defined for each instance of the object.

Here is a simple class for a Dog within a CodeBlock’s project; This simple class defines a Dog. Every dog has a name and every dog can bark:
// Dog.h

#ifndef DOG_H #define DOG_H #include <iostream> using namespace std;

class Dog

{

public:

Dog(string); //constructor ~Dog(); //destructor void bark();

protected:

private:

string name;

};

#endif // DOG_H

//Dog.cpp

#include "Dog.h" //make sure to include the header file #include <iostream>

using namespace std;

Dog::Dog(string dogName){ //ctor

name = dogName;

}

Dog::~Dog(){

//dtor

}

void Dog::bark(){

cout << "My dog's name is: " << name << endl;

}
//main.cpp

#include <iostream> #include "Dog.h" using namespace std;

int main(){

Dog DogOne("Scruffy"); DogOne.bark();

return 0;

}

After build and run is selected this will produce:

My dog’s name is: Scruffy

Notice a few things here. First, ​Dog is now a class (i.e. a valid type) and an object ( i.e. variable) of this class called DogOne. C++ knows what a ​Dog is, because we defined it above. To call one of the ​Dog object’s methods, simply type the variable name (indicating ​which object instance you want to bark) followed by a dot, followed by the method name.

Creating a class file

In code blocks, you can create the project, and add a class to that project. After the project is created, select File, New, and Class. This brings you to another menu where you can define your class name, add any desired arguments, and de-select the virtual destructor. It will automatically auto-populate the remaining fields as necessary. Then select Create. This will create a new .cpp file and a .h file(header file) that will be added to your existing main.cpp. main.cpp will remain the same and will be where you can create instances of the class and call the instances. The next menus will ask you if you want to add to existing, select yes.

public vs private

Notice in the Dog class that the attribute ​name has the keyword ​private in its declaration. Often (though not always), an object doesn’t want other objects or methods to be able to access these attributes directly. By applying the ​private keyword to an
attribute or method, that attribute can ​only be accessed by the object the attribute belongs to, e.g. only methods in Dog can access private Dog attributes.

Recap:

● 	private members of a class are accessible only from within other members of the same class (or from their ​"friends"). 
● 	public members are accessible from anywhere where the object is visible. 

Getters and Setters

Typically, a class may provide functions to get or set the values of an attribute (called getters and ​setters). One reason for doing this is to ensure that the assignment is a valid one for the attribute. Let’s modify the Dog class to have a getter and a setter for the variable.

//Dog.h ​-- for the header we have to add in the methods to the public section
#ifndef DOG_H #define DOG_H #include <iostream> using namespace std;

class Dog{ public:

Dog(string);

~Dog(); void bark();

string getName(); void setName(string);

protected:

private:

string name; string newName;

};

#endif // DOG_H

//Dog.cpp ​-- for this file we have to add the getter and setter methods #include "Dog.h"

#include <iostream>
using namespace std;

Dog::Dog(string dogName){ name = dogName;

}

Dog::~Dog(){

//dtor

}

void Dog::bark(){

cout << "My dog's name is: " << name << endl;

}

//getter​-- gets the desired value string Dog::getName(){

return name;

}

//setter ​-- sets the value as requested void Dog::setName(string newName){

name = newName;

}

//main.cpp ​-- for this main, we still make the instance of the Dog class, but then get the method and print it to the terminal

#include <iostream> #include "Dog.h" using namespace std;

int main(){

Dog DogOne("Scruffy");

cout << DogOne.getName() << endl;

DogOne.setName("Roscoe");

cout << DogOne.getName() << endl;

return 0;

}

After build and run is selected this will produce: Scruffy

Roscoe

Scruffy is from the first “getter” and Roscoe is from the “setter” and printed with the getter.

Recitation Activity

For today’s recitation activity ​you are going to create a class called battleShip.
First create the Codeblocks project. Name your project Recitation7.cpp. Now create the class file based on the instructions earlier in the recitation and name the class battleShip. When you create your class, you can set up your constructor and destructor at this time and it will populate these methods for you when it creates your class.

The​​battleShip.h file has been made available on Moodle. Cut and paste the code from that file into the class you just created. You will then create your class file with a number of methods. (Remember: methods define what your object ​does). How many methods do you see in your header file? It will be your job to fill in these methods so that they work. In battleShip.cpp you will define the methods.The constructor always has the same name as your class file, and it never has a data type. Make sure to import the proper class file and libraries in the header of your ​battleShip.cpp​file ​. ​You will also be given the main.cpp. Cut and paste the code from that file into the class you just created. Make sure to include the appropriate headers.

See the main.cpp for further instructions. You will need to create 3 instances of your class, set up some of the methods, and expand on the logic of the while loop.

battleShip.h will look like this:

#ifndef BATTLESHIP_H #define BATTLESHIP_H

#include <iostream> using namespace std;

class battleShip{ public:

battleShip(string);

~battleShip();

void setShipName(string); string getShipName();

void setSize(int); int getSize();

void recordHit(); bool isSunk();

private:

string name; int size;
int hits;

};

#endif // BATTLESHIP_H

When you have finished, implementing these methods, show your TA to check off your work and submit to moodle by Sunday at 5pm.